<template>
  <div>

    <div class="block" style="float: left;margin-top: 20px;margin-left: 10px;">
      <span class="demonstration"></span>
      <el-date-picker v-model="date" type="date" placeholder="选择日期" format="YYYY-MM-DD" value-format="YYYY-MM-DD"
        @change="getDate">
      </el-date-picker>
    </div>
    <div style="float: left;margin-top: 25px;margin-left: 10px;">
      开始时间 ：{{ startTime }} 结束时间 ：{{ endTime }}
    </div>


    <div>
      <span style="font-size: 8px;float: right;margin-right: 70px;margin-top: 30px;">
        <div style="background-color: #276a42;width: 20px;height: 20px;float: left;margin-right: 10px;"></div>有视频
      </span>
      <span style="font-size: 5px;float: right;margin-right: 50px;margin-top: 30px;">
        <div style="background-color: #283032;width: 20px;height: 20px;float: left;margin-right: 10px;"></div>无视频
      </span>
    </div>
    <div class="AxisData">
      <div class="timeData" v-for="(item, index) in list" :key="index">
        <div class="item" :style="{ left: item.left, width: item.width }"></div>
      </div>
      <el-slider class="self_slider" v-model="value1" :max="86400" :min="0" range :step="1" @change="onchange($event)"
        :format-tooltip="formatTooltip" />
    </div>
    <div v-for="(item, index) in timelist" style="float: left;color: aliceblue;">
      <div style="width: 1px;height: 3px;background-color: aliceblue;margin-left: 10px;margin-top: 20px;"></div>
      <span style="font-size: 16px;padding-right: 36px;">{{ item }}</span>
    </div>
  </div>
</template>

<script>
import { ElButton, ElNotification } from 'element-plus';
import api from "@/api/api";
export default {
  props: ["clientId"],
  name: 'TimeLine',

  data: function () {
    return {
      value1: [0, 0],
      timelist: [],
      startTime: "00:00:00",
      endTime: "00:00:00",
      list: [{
        width: 0,
        left: 0
      }],
      date: '',
      old: [-1, -1]
    }
  },
  computed: {


  },
  watch: {
    clientId() {
      this.list = [{
        width: 0,
        left: 0
      }];
      this.value1 = [0, 0]
      this.old = [-1, -1]
      this.startTime = "00:00:00",
        this.endTime = "00:00:00",
        this.playTime();
    },
    date() {
      this.list = [{
        width: 0,
        left: 0
      }];
      this.value1 = [0, 0]
      this.old = [-1, -1]
      this.startTime = "00:00:00",
        this.endTime = "00:00:00",
        this.playTime();
    }
  },
  emits: ["time", "date"],
  mounted() {
    //生成下面的24小时
    this.generateTimes();
    //给时间组件赋值当前时间
    this.getNowDate();
  },
  methods: {
    //获取播放时间
    playTime() {
      var dates = (this.date + "").replaceAll("-", "").slice(2)
      api.selectBackTime(this.clientId, 1, dates + "000000", dates + "235959", 0, 0, 0, 0, 0).then(res => {
        if (res.code == 0) {
          if (res.data.count == 0) {
            ElNotification({ title: 'Error', message: '没有回放录像', type: 'error', })
          } else {
            for (var i = 0; i < res.data.count; i++) {
              var s = res.data.items[i].startTime.slice(10)
              var e = res.data.items[i].endTime.slice(10)
              var ss = this.times(s)
              var es = this.times(e)
              var width = (es - ss) / 864;
              var left = (ss / 864);
              this.list.push({
                width: width + "%",
                left: left + "%"
              })
            }
          }

        }
      }).catch(err => {
        console.log(err)
      })

    },
    //24小时
    generateTimes() {
      let timeArrays = new Array(24).fill("");
      timeArrays.forEach((item, index) => timeArrays[index] = (index < 10 ? '0' + index : index) + ':00');
      this.timelist = timeArrays;
    },
    //进度条拖拽事件
    onchange(val) {
      const customClassbar = document.getElementsByClassName('el-slider__bar')
      const customClasswrapper = document.getElementsByClassName('el-slider__button-wrapper')
     // const customClasshover = document.getElementsByClassName('el-slider__button-wrapper hover')
      if (this.old[0] == -1 && this.old[1] == -1) {
        this.old = val;
        if (val[1] - val[0] > 1200) {
          var left = (val[1] / 864);
          val[0] = val[1] - 1200;
          customClassbar[0].style.width = (1200 / 864) + "%";
          customClassbar[0].style.left = (left - (1200 / 864)) + "%";
          customClasswrapper[0].style.left = (left - (1200 / 864)) + "%";
          this.value1[1] = val[1]
          this.value1[0] = val[0]
        }
      } else {
        if (this.old[1] < val[1]) {
          if (val[1] - val[0] > 1200) {
            var left = (val[1] / 864);
            val[0] = val[1] - 1200;
            customClassbar[0].style.width = (1200 / 864) + "%";
            customClassbar[0].style.left = (left - (1200 / 864)) + "%";
            customClasswrapper[0].style.left = (left - (1200 / 864)) + "%";
            this.value1[1] = val[1]
            this.value1[0] = val[0]
          }
        } else {
          console.log()
          if (val[1] - val[0] > 1200) {
            var left = (val[0] / 864);
            val[1] = val[0] + 1200;
            customClassbar[0].style.width = (1200 / 864) + "%";
            customClassbar[0].style.left = (left + (1200 / 864)) + "%";
            customClasswrapper[1].style.left = (left + (1200 / 864)) + "%";
            this.value1[1] = val[1]
            this.value1[0] = val[0]
          }
        }
      }
      this.old = val;
      this.startTime = this.formatTooltip(val[0])
      this.endTime = this.formatTooltip(val[1])
      // }
      this.$emit('time', [this.date + " " + this.startTime, this.date + " " + this.endTime]);
    },
    times(val) {
      let [hours, minutes, seconds] = val.split(':');
      let totalSeconds = (+hours) * 60 * 60 + (+minutes) * 60 + (+seconds);
      return totalSeconds;
    },
    //秒转化为时间
    formatTooltip(val) {
      let h = parseInt(val / 60 / 60 % 24)
      let m = parseInt(val / 60 % 60)
      let s = parseInt(val % 60)
      // 因为h已经是数字型了，如果0不加引号就变成加法了
      h = h < 10 ? '0' + h : h
      m = m < 10 ? '0' + m : m
      s = s < 10 ? '0' + s : s
      let time = h + ':' + m + ":" + s
      return time
    },
    getTime(time) {
      const timeArr = this.time.split(":");
      const hour = parseInt(timeArr[0]);
      const minute = parseInt(timeArr[1]);
      const second = parseInt(timeArr[2]);
      const totalSeconds = hour * 3600 + minute * 60 + second;
      return {
        hour,
        minute,
        second,
        totalSeconds
      };
    },
    getNowDate() {
      const timeOne = new Date()
      const year = timeOne.getFullYear()
      let month = timeOne.getMonth() + 1
      let day = timeOne.getDate()
      month = month < 10 ? '0' + month : month
      day = day < 10 ? '0' + day : day
      this.date = `${year}-${month}-${day}`
    },
    getDate(val) {
      this.date = val
      this.list = [{
        width: 0,
        left: 0
      }];
      this.playTime();
      this.$emit('date', this.date);
    }
  },
}

</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.AxisData {
  background-color: #111010;

  margin-left: 10px;
  margin-right: 10px;
}

.timeData {
  position: absolute;
  height: 40px;
  width: 96.6%;
  margin-top: 72px;
  z-index: 2;
}

.item {
  background-color: #276a42;
  height: 40px;
  z-index: 2;
  position: absolute;
}

.self_slider {
  width: 99.8%;
  height: 40px;
  background-color: #111010;
  position: relative;
}

:deep(.el-slider) {
  --el-slider-main-bg-color: #276a42;
}

:deep(.el-slider__runway) {
  height: 40px;
  margin-top: 40px;
  background-color: #283032;

}

:deep(.el-slider__bar) {

  height: 40px;
}

:deep(.el-slider__button-wrapper) {
  margin-top: 19px;
  border: none;

}

:deep(.el-slider__marks) {
  margin-top: 40px;
}

/* :deep(.el-slider__stop) {
  background-color: transparent;
} */

:deep(.el-slider__bar) {
  background: rgba(255, 0, 0, 0.315);
  z-index: 10;
  width: 100px;
  border-left: 2px solid red;
  border-right: 2px solid red;
}


:deep(.el-slider__button) {
  height: 0px;
  width: 0px;
  border: none;

}

:deep(.el-slider__button:hover ){
  background-color: #66B1FF;
  border-color: #66B1FF;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
}

/* 设置滑块轴的悬停效果 */
</style>
